#include "ImageCanvas.h"
#include "MainFrame.h"

BEGIN_EVENT_TABLE(ImageCanvas, wxWindow)
    EVT_PAINT  (ImageCanvas::OnPaint)
	EVT_SIZE   (ImageCanvas::OnSize)
	EVT_LEFT_DOWN(ImageCanvas::OnLbuttonDown)
	EVT_RIGHT_DOWN(ImageCanvas::OnRbuttonDown)
	EVT_LEFT_UP(ImageCanvas::OnLbuttonUp)
   EVT_MOTION (ImageCanvas::OnMouseMove)
END_EVENT_TABLE()


ImageCanvas::ImageCanvas(MainFrame *parent, int w, int h)
: wxWindow(parent, wxID_ANY, wxDefaultPosition, wxSize(w,h),wxFULL_REPAINT_ON_RESIZE)
,presenter(0L),
model(0L)

{
	flag = 0;
    m_owner = parent;
//    m_show = Show_Default;
  //  m_smile_bmp = wxBitmap(smile_xpm);
    //m_std_icon = wxArtProvider::GetIcon(wxART_INFORMATION);
    //m_clip = false;
//	m_pImage = 0L;
	//int width,height;
	//this->GetSize(&width,&height);

	presenter  = new GraphicImagePresenter(m_hWnd, w,h,"OPEN_GL");
	GraphicImageModel * model = new GraphicImageModel();
	model->GetImageGraph()->SetImage(parent->GetImage());
	presenter->SetModel(model);
	m_zoom = std::auto_ptr<NOVA::GraphicImageZoomController>(new NOVA::GraphicImageZoomController(presenter));
}
void ImageCanvas::OnSize(wxSizeEvent& sizeevent)
{
	wxWindow::SetSize(sizeevent.GetSize());
	presenter->GetRenderer()->GetContext()->OnSizeChanged(sizeevent.GetSize().GetWidth(),sizeevent.GetSize().GetWidth());
}
void ImageCanvas::OnPaint(wxPaintEvent &paintevent)
{
	wxPaintDC dc( this );
	PrepareDC( dc );

	//dc.DrawText( _T("Loaded image"), 30, 10 );

	if(presenter)
		presenter->Update();
//	char str[128];
//	sprintf(str,"%d\n",flag);
//	wxWindow::OnPaint(paintevent);
//	OutputDebugStringA(str);

//	if(flag++>200) exit(0);
	//int w,h;
	//this->GetSize(&w,&h);
	//presenter = new NOVA::GraphicImagePresenter(this->GetHWND(),w,h);
	//if(m_pImage)
	//{
	//	GraphicImageModel * model = new GraphicImageModel();
	//	model->GetImageGraph()->SetImage(m_pImage);
	//	if(presenter!=NULL&&presenter->GetModel()!=NULL)
	//	{
	//		presenter->SetModel(model);		
	//	   presenter->Update();
	//	}
	//paintevent.StopPropagation();
	//}
	//wxPaintDC dc(this);
	//using namespace NOVA;

	//if(m_pImage){

	//	wxRect rect = GetClientRect();

	//	if(rect.width == 0 || rect.height == 0)
	//	{
	//		return;
	//	}

	//	// Get the handle of the wxWindow
	//	HWND hwnd = (HWND)this->GetHandle();

	//	// Now get the native windows DC for this window
	//	HDC hdc = ::GetDC(hwnd);

	//	int w  = m_pImage->GetWidth();
	//	int h = m_pImage->GetHeight();


	//	HDC dcbuffer = CreateCompatibleDC(hdc);
	//	HBITMAP hbuffer = CreateCompatibleBitmap(hdc, w, h);
	//	SelectObject(dcbuffer, hbuffer); 

	//	GraphicImageModel * model = new GraphicImageModel();
	//	

	//	model->GetImageGraph()->SetImage(m_pImage);

	//	GraphicImagePresenter* p = new GraphicImagePresenter(dcbuffer,hbuffer);
	//	p->SetModel(model);
	//	p->OnPaint();

	//	// Now blit the object to the screen
	//	BitBlt(hdc, 0, 0, rect.width, rect.height, dcbuffer, 0, 0, SRCCOPY);



	//	DeleteDC(dcbuffer);
	//	DeleteObject(hbuffer);

	//	// Because we called ::GetDC make sure we release the handle
	//	// back to the system or we'll have a memory leak.
	//	::ReleaseDC(hwnd,hdc);

	//	delete model;
	//	delete p;
	//}
}

void ImageCanvas::OnRbuttonDown(wxMouseEvent &event)
{
	;
}
void ImageCanvas::OnLbuttonDown(wxMouseEvent &event)
{
	if(m_zoom.get())
	{ 
		int x,y;
		event.GetPosition(&x,&y);
		int pos[2] = {x,y};
		m_zoom.get()->OnMouseDown(pos);
	}  
	
}
void ImageCanvas::OnLbuttonUp(wxMouseEvent &event)
{
	if(m_zoom.get())
	{ 
		int x,y;
		event.GetPosition(&x,&y);
		int pos[2] = {x,y};
		m_zoom.get()->OnMouseUp(pos);
	}  

}
void ImageCanvas::OnMouseMove(wxMouseEvent &event)
{
	if(m_zoom.get())
	{ 
		int x,y;
		event.GetPosition(&x,&y);
		int pos[2] = {x,y};
		m_zoom.get()->OnMouseMove(pos);
	}  
}